// 统一用户管理的接口

import request from '@/utils/service'
import type { ILoginDto, IUser } from '@/api/types'
type IUpdateUserDto = Partial<IUser> & { password?: string }
enum API {
  LOGIN_URL = '/acl/auth/login',
  LOGOUT_URL = '/acl/auth/logout',
  USER_BY_ID_URL = '/system/user/detail/',
  USER_UPDATE_URL = '/system/user/update/',
  ROLE_DETAIL_URL = '/system/role/detail/',
  MENU_LIST_IDS_URL = '/system/menu/listByIds'
  // UPLOAD_AVATAR_URL = '/upload/album'
}

/**
 * 用户登录
 * @param data 用户登录信息
 * @returns
 */
export const reqLogin = (data: ILoginDto) => {
  const url = API.LOGIN_URL
  return request.post({ url, data })
}

/**
 * 通过用户id注销
 * @param id 用户id
 * @returns
 */
export const reqLogout = (id: string) => {
  const url = API.LOGOUT_URL
  return request.post({ url, data: id })
}

/**
 * 通过用户id获取用户信息
 * @param id 用户id
 * @returns
 */
export const getUserById = (id: string) =>
  request.get({ url: API.USER_BY_ID_URL + id })

export const updateUserById = (id: string, data: IUpdateUserDto) => {
  const url = API.USER_UPDATE_URL + id
  return request.patch({ url, data })
}

/**
 * 通过角色id获取角色信息
 * @param id 角色id
 * @returns
 */
export const getRoleById = (id: string) => {
  const url = API.ROLE_DETAIL_URL + id
  return request.get({ url })
}

/**
 * 通过角色id获取菜单数组
 * @param ids 角色id数组
 * @returns
 */
export const getMenusByRoleIds = (ids: string[]) => {
  const url = API.MENU_LIST_IDS_URL
  return request.post({ url, data: ids })
}

// export const uploadAvatar = (data: File) => {
//   const formData = new FormData()
//   formData.append('file', data)
//   return request.post({
//     url: API.UPLOAD_AVATAR_URL,
//     data: formData,
//     headers: {
//       'Content-Type': 'multipart/form-data'
//     }
//   })
// }
